<?php
require_once ("../src/db/connectdatabase.php");
include_once('../model/SendMail.php');

function insertApplyCard($id, $fullname, $dayofbirth, $contry, $phonenumber, $email, $profession, $sumsalary, $idbank, $namebank, $logobank) {
	$con = new ConnectDB();
	
	$tbl = "applycard";
	$fn = array('aplly_id' => $id, 'fullname' => $fullname, 'dayofbirth' => $dayofbirth, 'country' => $contry, 'phone_number' => $phonenumber, 'email' => $email, 'profession' => $profession, 'tongluong' => $sumsalary, 'bankid' => $idbank, 'bankname' => $namebank, 'banklogo' => $logobank, 'datecreate' => $id);
	return $con -> insert($tbl, $fn);
}

function getBank($idbank) {
	$con = new ConnectDB();
	
	$tbl = "banks";
	$fn = array('bankid', 'bankname', 'banklogo');
	$condetion = array('bankid' => $idbank);
	return $con -> getvalue($tbl, $fn, $condetion);
}

$result = false;

if(isset($_POST['Name']) && isset($_POST['Phone']) && isset($_POST['Email']) && isset($_POST['City']) && isset($_POST['Net_Salary']) && isset($_POST['bank'])) {
	$nameloan = "";
	
	$fullname = $_POST['Name'];
	$phonenumber = $_POST['Phone'];
	$email = $_POST['Email'];
	$contry = $_POST['City'];
	$sumsalary = $_POST['Net_Salary'];// tong luong
	$dayofbirth = "";
	$profession = "";// nghe nghiep
	$idbank = $_POST['bank'];
	
	if(isset($_POST['birthday'])) {
		$dayofbirth = $_POST['birthday'];
	}
	if(isset($_POST['Employment_Status'])) {
		$profession = $_POST['Employment_Status'];
	}
	$bankarr = getBank($idbank);
	if(count($bankarr) > 0) {
		$id = time();
		$namebank = $bankarr[0][1];
		$logobank = $bankarr[0][2];
		//if(insertApplyCard($id, $fullname, $dayofbirth, $contry, $phonenumber, $email, $profession, $sumsalary, $idbank, $namebank, $logobank)) {
			$body = '<style>table tr {height: 30px;min-height: 30px;}</style><div style="border: 1px solid; width: 600px; min-height: 300px;"><div style="width: 100%; min-height: 50px; background: #8DAE48; text-align: center; margin-top: -20px;"><h2 style="color: #FFF; padding-top: 10px;">ĐĂNG KÝ THÔNG TIN THẺ TÍN DỤNG</h2></div><div style="min-height: 250px; padding: 5px;"><table><tr><th align="left" valign="top">Tên Khách Hàng:</th><td align="left" valign="top">'.$fullname.'</td></tr><tr><th align="left" valign="top">Ngày Sinh:</th><td align="left" valign="top">'.$dayofbirth.'</td></tr><tr><th align="left" valign="top">Hình Thức:</th><td align="left" valign="top"><strong>Đăng Ký Làm Thẻ Tín Dụng</strong></td></tr><tr><th align="left" valign="top">Số Điện Thoại:</th><td align="left" valign="top">'.$phonenumber.'</td></tr><tr><th align="left" valign="top">Email:</th><td align="left" valign="top">'.$email.'</td></tr><tr><th align="left" valign="top">Tỉnh Thành:</th><td align="left" valign="top">'.$contry.'</td></tr><tr><th align="left" valign="top">Nghề Nghiệp:</th><td align="left" valign="top">'.$profession.'</td></tr><tr><th align="left" valign="top">Ngân Hàng:</th><td align="left" valign="top">'.$namebank.'('.$idbank.')</td></tr><tr><th align="left" valign="top">Tổng Lương Tháng:</th><td align="left" valign="top">'.$sumsalary.'</td></tr></table></div></div>';
			
			if(smtpmailer($fullname."_Thẻ Tín Dụng", $body)) {
				$result = true;
			}
			echo json_encode($result);
			return;
		//}
	}
}

echo json_encode($result);
?>